<template>
  <div class="app-container">
    <div class="page-header">
      <div class="wallet-info">
        <i class="el-icon-wallet"></i>
        <span>钱包余额:</span>
        <span class="balance" v-if="walletInfo.balance !== undefined">¥ {{ Number(walletInfo.balance).toFixed(2) }}</span>
        <span class="balance" v-else><el-skeleton-item variant="text" style="width: 60px" /></span>
        <el-button type="text" size="mini" icon="el-icon-refresh" @click="getWalletInfo" :loading="walletLoading">刷新</el-button>
      </div>
    </div>
    <el-row :gutter="20">
      <el-col :span="6" :xs="24">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>个人信息</span>
          </div>
          <div>
            <div class="text-center">
              <userAvatar />
            </div>
            <ul class="list-group list-group-striped">
              <li class="list-group-item">
                <svg-icon icon-class="user" />用户名称
                <div class="pull-right">{{ user.userName }}</div>
              </li>
              <li class="list-group-item">
                <svg-icon icon-class="phone" />手机号码
                <div class="pull-right">{{ user.phonenumber }}</div>
              </li>
              <li class="list-group-item">
                <svg-icon icon-class="email" />用户邮箱
                <div class="pull-right">{{ user.email }}</div>
              </li>
              <li class="list-group-item">
                <svg-icon icon-class="tree" />所属部门
                <div class="pull-right" v-if="user.dept">{{ user.dept.deptName }} / {{ postGroup }}</div>
              </li>
              <li class="list-group-item">
                <svg-icon icon-class="peoples" />所属角色
                <div class="pull-right">{{ roleGroup }}</div>
              </li>
              <li class="list-group-item">
                <svg-icon icon-class="tree" />区域
                <div class="pull-right" v-if="user.area">{{ user.area }}</div>
              </li>
              <li class="list-group-item">
                <svg-icon icon-class="date" />创建日期
                <div class="pull-right">{{ user.createTime }}</div>
              </li>
            </ul>
          </div>
        </el-card>
      </el-col>
      <el-col :span="18" :xs="24">
        <el-card>
          <div slot="header" class="clearfix">
            <span>基本资料</span>
          </div>
          <el-tabs v-model="activeTab">
            <el-tab-pane label="基本资料" name="userinfo">
              <userInfo :user="user" />
            </el-tab-pane>

            <el-tab-pane label="修改密码" name="resetPwd">
              <resetPwd />
            </el-tab-pane>

            <el-tab-pane v-if="checkRole(['supplier-noAudited','supplier-retail','supplier-stratagem','supplier-guarantor'])" label="供应商信息认证"  name="sup_info">
              <sup_info  />
            </el-tab-pane>

            <el-tab-pane v-if="checkRole(['purchaser-noAudited','purchaser-Audited'])" label="采购商信息认证"  name="purch_info">
              <purch_info  />
            </el-tab-pane>

            <el-tab-pane v-if="checkRole(['supplier-retail'])" label="供应商身份升级"  name="purch_info_update">
              <sup_info_update  />
            </el-tab-pane>
          </el-tabs>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import userAvatar from "./userAvatar";
import userInfo from "./userInfo";
import resetPwd from "./resetPwd";
import sup_info from "./sup_info";
import purch_info from "./purch_info";
import { getUserProfile } from "@/api/system/user";
import { checkPermi, checkRole } from "@/utils/permission";
import request from "@/utils/request";
import sup_info_update from "./sup_info_update";

export default {
  name: "Profile",
  components: { userAvatar, userInfo, resetPwd,sup_info,purch_info,sup_info_update},
  data() {
    return {
      user: {},
      roleGroup: {},
      postGroup: {},
      activeTab: "userinfo",
      activeAuth:true,
      globalData: {
        supplierId: null,
        purchaserId: null
      },
      walletInfo: {
        balance: undefined
      },
      walletLoading: false
    };
  },
  provide() {
    return {
      globalData: this.globalData
    }
  },
  created() {
    this.getUser();
    this.getWalletInfo();
  },
  methods: {
    getUser() {
      getUserProfile().then(response => {
        this.user = response.data;
        this.roleGroup = response.roleGroup;
        this.postGroup = response.postGroup;
        this.globalData.supplierId = this.user.userId;
        this.globalData.purchaserId = this.user.userId;
      });
    },
    checkRole,
    checkPermi,
    getWalletInfo() {
      this.walletLoading = true;

      request.get('/supply/wallet').then(response => {
        if (response.code === 200) {
          console.log(Number(response.data.balance))
          this.walletInfo.balance = Number(response.data.balance || 0);
          console.log('获取钱包余额成功:', this.walletInfo.balance);
        } else {
          console.error('获取钱包余额异常:', response);
          this.$message.warning('获取钱包余额异常');
          this.walletInfo.balance = 0;
        }
      }).catch(error => {
        console.error('获取钱包余额失败:', error);
        this.$message.error('获取钱包余额失败');
        this.walletInfo.balance = 0;
      }).finally(() => {
        this.walletLoading = false;
      });
    }
  }
};
</script>


<style>
  .balance {
    color: #4CAF50; /* 修改为蓝色 */
  }
</style>
